<template>
    <div v-if="isAuth(['ROOT', 'MEDICAL_DEPT:SELECT',])">
		<el-form :inline="true" :model="dataForm" :rules="dataRule" ref="dataForm">
		    <el-form-item prop="name">
		        <el-input
		            v-model="dataForm.drugname"
		            placeholder="药品名称"
		            size="medium"
		            class="input"
		            clearable="clearable"
		        />
		    </el-form-item>
			<el-form-item prop="drugid">
			    <el-input
			        v-model="dataForm.drugid"
			        placeholder="药品编号"
			        size="medium"
			        class="input"
			        clearable="clearable"
			    />
			</el-form-item>
			<el-form-item>
			    <el-button type="primary" @click="searchHandle()">查询</el-button>
			</el-form-item>
		</el-form>
        <el-table
            :data="dataList"
            border
            v-loading="dataListLoading"
            @selection-change="selectionChangeHandle"
            :cell-style="{ padding: '3px 0' }"
            style="width: 100%;"
        >
            <el-table-column
                type="selection"
                :selectable="selectable"
                header-align="center"
                align="center"
                width="50"
            />
            <el-table-column type="index" header-align="center" align="center" width="100" label="序号">
                <template #default="scope">
                    <span>{{ (pageIndex - 1) * pageSize + scope.$index + 1 }}</span>
                </template>
            </el-table-column>
			<el-table-column header-align="center" align="center" label="药品编号" min-width="180">
			    <template #default="scope">
			        <span>{{ scope.row.drugid }}</span>
			    </template>
			</el-table-column>
			<el-table-column header-align="center" align="center" label="药品名称" min-width="120">
			    <template #default="scope">
			        <span>{{ scope.row.drugname }}</span>
			    </template>
			</el-table-column>
			<el-table-column header-align="center" align="center" label="药品数量" min-width="120">
			    <template #default="scope">
			        <span>{{ scope.row.drugnum }}</span>
			    </template>
			</el-table-column>
			<el-table-column header-align="center" align="center" width="150" label="操作">
			    <template #default="scope">
			        <el-button
			            type="text"
			            size="medium"
			            :disabled="!isAuth(['ROOT'])"
			            @click="addDrugNum(scope.row.id)"
			        >
			            入库
			        </el-button>
			        <el-button
			            type="text"
			            size="medium"
			            :disabled="!isAuth(['ROOT'])"
			            @click="reduceNum(scope.row.id)"
			        >
			            出库
			        </el-button>
			    </template>
			</el-table-column>
			</el-table>
        <el-pagination
            @size-change="sizeChangeHandle"
            @current-change="currentChangeHandle"
            :current-page="pageIndex"
            :page-sizes="[10, 20, 50]"
            :page-size="pageSize"
            :total="totalCount"
            layout="total, sizes, prev, pager, next, jumper"
        ></el-pagination>

        <num-add ref="numAdd" @refreshDataList="loadDataList"></num-add>
		<num-reduce ref="numReduce" @refreshDataList="loadDataList"></num-reduce>
    </div>
</template>

<script>
import NumAdd from './drug-num-add.vue';
import NumReduce from './drug-num-reduce.vue'
export default {
    components: {
        NumAdd,
		NumReduce
    },
    data: function() {
        return {
            dataForm: {
				id:'',
                drugname: '',
                drugid: '',
				drugnum: ''
            },
            dataList: [],
            pageIndex: 1,
            pageSize: 10,
            totalCount: 0,
            dataListLoading: false,
            dataListSelections: [],
            dataRule: {
                name: [{ required: false, pattern: '^[\u4e00-\u9fa5]{1,10}$', message: '药品名称格式错误' }],
				drugid: [{ required: false, pattern: '^[0-9]{1,13}$', message: '药品编号格式错误' }]
            }
        };
    },
    methods: {
        loadDataList: function() {
            let that = this;
            that.dataListLoading = true;
            let data = {
                page: that.pageIndex,
                length: that.pageSize,
				drugname: that.dataForm.drugname == '' ? null : that.dataForm.drugname,
				drugid: that.dataForm.drugid == '' ? null : that.dataForm.drugid
            };
        
            that.$http('/drugnum/searchNumByPage', 'POST', data, true, function(resp) {
                let result = resp.result;
                that.dataList = result.list;
                that.totalCount = result.totalCount;
                that.dataListLoading = false;
            });
        },
		sizeChangeHandle: function(val) {
		    this.pageSize = val;
		    this.pageIndex = 1;
		    this.loadDataList();
		},
		currentChangeHandle: function(val) {
		    this.pageIndex = val;
		    this.loadDataList();
		},
		addDrugNum: function(id) {
		    //使用$nextTick函数可以保证init函数中的代码都能执行完，不会强制结束
		   this.$nextTick(() => {
		        this.$refs.numAdd.init(id);
		   });
		},
		reduceNum: function(id) {
		    //使用$nextTick函数可以保证init函数中的代码都能执行完，不会强制结束
		   this.$nextTick(() => {
		        this.$refs.numReduce.init(id);
		   });
		},
		searchHandle: function() {
		    this.$refs['dataForm'].validate(valid => {
		        if (valid) {
		            this.$refs['dataForm'].clearValidate();
		            if (this.dataForm.drugname == '') {
		                this.dataForm.drugname = null;
		            }
					if (this.dataForm.drugid == '') {
					    this.dataForm.drugid = null;
					}
		            if (this.pageIndex != 1) {
		                this.pageIndex = 1;
		            }
		            this.loadDataList();
		        } else {
		            return false;
		        }
		    });
		},
		selectionChangeHandle: function(val) {
		    this.dataListSelections = val;
		},
    },
    created: function() {
        this.loadDataList();
    }
};
</script>

<style></style>
